package top.juntower.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.juntower.pojo.EnjoyFoot;
import top.juntower.pojo.vo.EnjoyFootVo;
import top.juntower.service.impl.EnjoyFootServiceImpl;
import top.juntower.utils.JsonResult;
import top.juntower.utils.RedisKeyUtils;
import top.juntower.utils.RedisUtils;

import java.util.List;

@Api(tags = "食物喜欢")
@RestController
@RequestMapping("enjoyfoot")
public class EnjoyFootController {

    @Autowired
    EnjoyFootServiceImpl enjoyFootService;

    @Autowired
    private RedisUtils redisUti;

    //用户喜欢哪些食物
    @ApiOperation(value = "用户喜欢哪些食物",response = EnjoyFoot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", dataType = "Integer", required = true),
    })
    @GetMapping("/queryFavoriteFoot")
    public JsonResult queryFavoriteFoot(Integer userId){
        if (userId == null ){
            return  JsonResult.error(JsonResult.NO);
        }
        List<EnjoyFootVo> enjoyFootVos = enjoyFootService.queryFavoriteFoot(userId);
        if (enjoyFootVos == null){
            return JsonResult.error(JsonResult.MISS);
        }else {
            return JsonResult.ok(enjoyFootVos);
        }
    }

    //用户点击状态
    @ApiOperation(value = "用户点击状态",response = EnjoyFoot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "fId", value = "食物Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "efState", value = "喜欢状态", dataType = "String", required = true),
    })
    @GetMapping("/isState")
    public JsonResult isState(Integer userId,Integer fId,String efState){
        Boolean aBoolean  = false;
        if (userId == null || fId == null || efState == null ){
            JsonResult.error(JsonResult.NO);
        }
        String likedKey = RedisKeyUtils.getLikedKey(userId,fId);
        redisUti.hset("isState", likedKey, efState,5);
        if (efState.equals("0")){
            return JsonResult.ok(aBoolean);
        }else {
            return JsonResult.ok(!aBoolean);
        }
    }

}
